import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard', affix: true }
    }]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
// export const asyncRoutes = [
//   {
//     path: '/core/sys',
//     component: Layout,
//     redirect: 'noRedirect',
//     name: 'dict',
//     meta: { title: '系统管理', icon: 'el-icon-setting' },
//     alwaysShow: true,
//     children: [
//       {
//         path: 'dict-list',
//         name: 'dictList',
//         component: () => import('@/views/core/sys/dict-list'),
//         meta: { title: '数据字典' }
//       },
//       {
//         path: 'authority',
//         component: () => import('@/views/core/sys/authority/index'),
//         name: 'authority',
//         redirect: 'noRedirect',
//         meta: { title: '权限管理' },
//         children: [
//           {
//             path: 'admin-user',
//             component: () => import('@/views/core/sys/authority/admin-user'),
//             name: 'admin-user',
//             meta: { 
//               title: '用户管理',
//               roles: [
//                 'admin:user:list', 
//                 'admin:user:saveOrUpdate',
//                 'admin:user:info',
//                 'admin:user:remove'
//               ]
//             }
//           },
//           {
//             path: 'admin-role',
//             component: () => import('@/views/core/sys/authority/admin-role'),
//             name: 'admin-role',
//             meta: { 
//               title: '角色管理',
//               roles: [
//                 'admin:role:list',
//                 'admin:role:info',
//                 'admin:role:saveOrUpdate',
//                 'admin:role:remove',
//                 'admin:role:getRoleAll'
//               ]
//             }
//           },
//           {
//             path: 'admin-menu',
//             component: () => import('@/views/core/sys/authority/admin-menu'),
//             name: 'admin-menu',
//             meta: { 
//               title: '菜单管理',
//               roles: [
//                 'admin:menu:list',
//                 'admin:menu:upMenus',
//                 'admin:menu:info',
//                 'admin:menu:saveOrUpdate',
//                 'admin:menu:remove'
//               ]
//             }
//           }
//         ]
//       },
//       {
//         path: 'list',
//         name: 'quartzList',
//         component: () => import('@/views/core/sys/quartz/list'),
//         meta: { 
//           title: '定时任务',
//           roles: [
//             'admin:quartz:list',
//             'admin:quartz:info',
//             'admin:quartz:saveOrUpdate',
//             'admin:quartz:remove',
//             'admin:quartz:stopJob',
//             'admin:quartz:resumeJob',
//             'admin:quartz:runJob',
//             'admin:quartzLog:list'
//           ]
//         }
//       },
//     ]
//   },

//   {
//     path: '/core/integral-grade',
//     component: Layout,
//     redirect: 'noRedirect',
//     name: 'coreIntegralGrade',
//     meta: { title: '积分等级管理', icon: 'el-icon-s-marketing' },
//     alwaysShow: true,
//     children: [
//       {
//         path: 'list',
//         name: 'coreIntegralGradeList',
//         component: () => import('@/views/core/integral-grade/list'),
//         meta: { title: '积分等级列表' }
//       },
//       {
//         path: 'create',
//         name: 'coreIntegralGradeCreate',
//         component: () => import('@/views/core/integral-grade/form'),
//         meta: { title: '新增积分等级' }
//       },
//       {
//         path: 'edit/:id',
//         name: 'coreIntegralGradeEdit',
//         component: () => import('@/views/core/integral-grade/form'),
//         meta: { title: '编辑积分等级' },
//         hidden: true
//       }
//     ]
//   },

//   {
//     path: '/core/ums',
//     component: Layout,
//     redirect: 'noRedirect',
//     name: 'ums',
//     meta: { title: '商品管理', icon: 'el-icon-goods' },
//     alwaysShow: true,
//     children: [
//       {
//         path: 'spu-list',
//         name: 'spuList',
//         component: () => import('@/views/core/pms/spu-list'),
//         meta: { title: '商品列表' }
//       },
//       {
//         path: 'classify-list',
//         name: 'classifyList',
//         component: () => import('@/views/core/pms/classify-list'),
//         meta: { title: '分类列表' }
//       },
//       {
//         path: 'brand',
//         name: 'brand',
//         component: () => import('@/views/core/pms/brand'),
//         meta: { title: '品牌管理' }
//       },
//       {
//         path: 'attr',
//         component: () => import('@/views/core/pms/attr/index'),
//         name: 'attr',
//         redirect: 'noRedirect',
//         meta: { title: '平台属性' },
//         children: [
//           {
//             path: 'spu-attr-group',
//             component: () => import('@/views/core/pms/attr/spu-attr-group'),
//             name: 'apuAttrGroup',
//             meta: { 
//               title: '属性分组'
//             }
//           },
//           {
//             path: 'spu-attr',
//             component: () => import('@/views/core/pms/attr/spu-attr'),
//             name: 'spuAttr',
//             meta: { 
//               title: '规格参数'
//             }
//           },
//           {
//             path: 'spu-attr1',
//             component: () => import('@/views/core/pms/attr/spu-attr'),
//             name: 'spuAttr1',
//             meta: { 
//               title: '销售属性'
//             }
//           }
//         ]
//       }
//     ]
//   },

//   {
//     path: '/core/sys/generator',
//     component: Layout,
//     redirect: 'noRedirect',
//     name: 'generator',
//     meta: { title: '代码生成管理', icon: 'el-icon-s-marketing' },
//     alwaysShow: true,
//     children: [
//       {
//         path: 'data-source',
//         name: 'dataSource',
//         component: () => import('@/views/core/sys/generator/data-source'),
//         meta: { title: '数据源管理' }
//       },
//       {
//         path: 'data-list',
//         name: 'dataList',
//         component: () => import('@/views/core/sys/generator/data-list'),
//         meta: { title: '代码生成管理' }
//       }
//     ]
//   },

//   // 404 page must be placed at the end !!!
//   { path: '*', redirect: '/404', hidden: true }
// ]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

// createRouter.beforeEach((to, from, next) => {
//   navigationApi.getNavigation().then(res => {
//     var menus = res.data.menus
//     var routes = []
//     for (var i=0;i<menus.length;i++) {
//       var menu = menus[i]
//       var data = {}
//       data.path = menu.menuPath
//       data.component = Layout
//       data.redirect = 'noRedirect'
//       data.name = menu.menuName
//       data.alwaysShow = true
//       var meta = {}
//       meta.title = menu.menuTitle
//       meta.icon = menu.menuIcon
//       data.meta = meta
//       if (menu.children.length > 0) {
//         var children = []
//         for (var j =0;j<menu.children.length;j++) {
//           var childrenMenu = menu.children[j]
//           var item = {
//             path: childrenMenu.menuPath,
//             name: childrenMenu.menuName,
//             component: () => import('@/views/' + childrenMenu.menuUrl),
//             meta: { 
//               title: childrenMenu.menuTitle,
//               roles: childrenMenu.perms
//             }
//           }
//           children.push(item)
//         }
//         data.children = children
//       }
//       routes.push(data)
//     }
//     console.log(routes)
//     routes.push({ path: '*', redirect: '/404', hidden: true })
//     next({ ...to, replace: true })
//   }).catch()
// })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
